package cn.ljy.uem.service.impl;

import cn.ljy.authority.service.impl.IBaseService;
import cn.ljy.common.exception.model.MyExcelException;
import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.common.utils.page.Pagination;
import cn.ljy.uem.dao.SchoolDepartmentMapper;
import cn.ljy.uem.model.SchoolClassRoom;
import cn.ljy.uem.model.SchoolDepartment;
import cn.ljy.uem.model.query.SchoolClassRoomQuery;
import cn.ljy.uem.service.SchoolClassRoomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * 学校教室impl
 *
 * @author 凡渊
 * @date 2022/07/21 10:35:50
 */
@Service
@Slf4j
public class ISchoolClassRoomService extends IBaseService<SchoolClassRoom, SchoolClassRoomQuery> implements SchoolClassRoomService {
    @Autowired
    SchoolDepartmentMapper schoolDepartmentMapper;

    @Override
    protected void beforeGet(Serializable id) {

    }

    @Override
    protected void beforeUpdate(SchoolClassRoom schoolClassRoom) {

    }

    @Override
    protected void beforeSave(SchoolClassRoom schoolClassRoom) {
       if(schoolClassRoom!=null){
                 //判断姓名不能为空
             if(schoolClassRoom.getName()==null){
                 throw new MyWebException("添加失败：教室名称为空");
                 //判断地点不能为空
             } else if (schoolClassRoom.getAddress()==null) {
                 throw new MyWebException("添加失败：教室地点为空");
                 //判断教室类型不能为空
             } else if (schoolClassRoom.getType()==null) {
                 throw new MyWebException("添加失败：教室类型为空");
                 //判断状态不能为空
             }
             if (schoolClassRoom.getExpired()==null) {
                schoolClassRoom.setExpired(false);
             }
       }else {
           throw new MyWebException("添加失败：添加对象为空");
       }
    }

    @Override
    protected void beforeDelete(Serializable[] ids) {

    }

/*    @Override
    public List<SchoolClassRoom> findAll(SchoolClassRoomQuery query) {
        List<SchoolClassRoom> all = super.findAll(query);
        for (SchoolClassRoom schoolClassRoom :
                all) {
            if (schoolClassRoom.getDepartmentId() != null) {
                SchoolDepartment schoolDepartment = schoolDepartmentMapper.findById(schoolClassRoom.getDepartmentId());
                schoolClassRoom.setSchoolDepartment(schoolDepartment);
            }
        }
        return all;
    }*/

    @Override
    protected void afterFindAll(List<SchoolClassRoom> list) {
        for (SchoolClassRoom schoolClassRoom :
                list) {
            if (schoolClassRoom.getDepartmentId() != null) {
                SchoolDepartment schoolDepartment = schoolDepartmentMapper.findById(schoolClassRoom.getDepartmentId());
                schoolClassRoom.setSchoolDepartment(schoolDepartment);
            }

        }
    }
}
